home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-07 | 8.4 KB | 290 lines | [TEXT/MPCC] |
- // ----------------------------------------------------------------------------------
- // EventLoop.c
- // ----------------------------------------------------------------------------------
- // Main event loop for a standard Macintosh application
- //
- // This demo is copywrite 1994 by LexTek Internation. Feel free to use it for
- // whatever purpose you wish. The SpellWright Library may not be copied or
- // distributed, except when linked to your application that adds significant features
- // to the code. ie. you can't take the library, make a new library and sell it. You
- // can, however, use it without royalties in applications or other such projects.
- //
- // LexTek International
- // 2255 N. University Parkway, Suit 15
- // Provo, UT 84604
- // (801) 375.8332 Phone
- // (801) 375.7654 Fax
- //
- // ----------------------------------------------------------------------------------
- // History:
- // 8/26/94 Clark Goble Original
- // 10/1/94 Clark Goble Finished cleaning up some of the bugs. Added cursor
- // hiding when a key is typed. Cleaned up Sys6 support.
- //
-
- #include "EventLoop.h"
- #include "AEHandler.h"
- #include "WindowHandler.h"
- #include "TEHandler.h"
- #include "Globals.h"
-
- void DoMenu(register long msel);
- void DoKey(register EventRecord *evt);
- void DrawClippedGrowIcon(WindowPtr theWindow);
- void DoUpdate(register EventRecord *evt);
- void DoActivate(register EventRecord *evt);
- void DoMulti(register EventRecord *evt);
- void DoClick(register EventRecord *evt);
- void DoEvent(void);
-
- // ----------------------------------------------------------------------------------
-
- extern void MenuHandler(short menuNumber,short itemNumber);
- extern void DrawImage(GrafPtr graf);
-
- // ----------------------------------------------------------------------------------
-
- Boolean gQuit = false; // Quit the program when true
- Boolean gHaveWNE = false; // Do we have WaitNextEvent
- Boolean gInBack = false; // True if we are in the background
- Boolean gSys7 = false; // Sys 7 available
-
-
- // ----------------------------------------------------------------------------------
- // DoMenu
- // ----------------------------------------------------------------------------------
- // Takes the menu selected and gets from it the menu id and the item id of that
- // menu. It then calls the appropriate menu handler.
-
- void DoMenu(register long msel)
- {
- int theItem;
- int theMenu;
-
- theItem = LoWord(msel);
- theMenu = HiWord(msel);
-
- MenuHandler(theMenu, theItem);
-
- HiliteMenu(0);
-
- } // DoMenu
-
- // ----------------------------------------------------------------------------------
- // DoKey
- // ----------------------------------------------------------------------------------
- // Handles a key command, sending it to either a text window or a menu.
-
- void DoKey(register EventRecord *evt)
- {
- char keyChar;
-
- keyChar = (char)evt->message & charCodeMask;
-
- if((evt->modifiers & cmdKey) == FALSE)
- { // no command key pressed
-
- DoTEKey(gDocWindow, keyChar );
- } else
- { // command key pressed
- DoMenu(MenuKey(keyChar));
-
- } // if/else
- } // DoKey
-
- // ----------------------------------------------------------------------------------
- // DrawClippedGrowIcon
- // ----------------------------------------------------------------------------------
- // Draws the grow icon, but clips it 'grow icon region.' (Makes it look a little
- // better.)
-
- void DrawClippedGrowIcon(WindowPtr theWindow)
-
- {
- Rect theClip;
- RgnHandle oldClip;
-
- oldClip = NewRgn(); // Save the old clipping region
- GetClip(oldClip);
-
- theClip = theWindow->portRect;
- theClip.left = theClip.right - 15;
- theClip.top = theClip.bottom - 15;
-
- ClipRect(&theClip);
- DrawGrowIcon(theWindow);
-
- SetClip(oldClip); // Restore the old clipping region
- } // DrawClippedGrowIcon
-
-
- // ----------------------------------------------------------------------------------
- // DoUpdate
- // ----------------------------------------------------------------------------------
- // Handle an update event. Here we merely clean up the window, call a TE update,
- // update the controls and other such things
-
- void DoUpdate(register EventRecord *evt)
- {
- WindowPtr updateWindow;
- GrafPtr savePort;
- TEHandle theTE;
-
-
-
-
- updateWindow=(WindowPtr)evt->message; // The window to update
- theTE = GetTE(updateWindow);
- BeginUpdate(updateWindow);
-
- GetPort(&savePort); // Save the port
- SetPort(updateWindow);
- EraseRect(&updateWindow->portRect); // Erase the window
- TEUpdate( &(updateWindow->portRect), theTE );
- DrawClippedGrowIcon(updateWindow); // Draw the grow icon
- DrawControls(updateWindow); // Draw the window's controls
- UpdateDocWindow(updateWindow);
- SetPort(savePort); // Restore the port
-
- EndUpdate(updateWindow);
-
- } // DoUpdate
-
-
- // ----------------------------------------------------------------------------------
- // DoActivate
- // ----------------------------------------------------------------------------------
- // Handle an activate event. It either activates or deactivates the window.
-
- void DoActivate(register EventRecord *evt)
- {
- if(evt->modifiers & activeFlag)
- ActivateWindow((WindowPtr )evt->message);
- else
- DeactivateWindow((WindowPtr )evt->message);
-
- } // DoActivate
-
- // ----------------------------------------------------------------------------------
- // DoMulti
- // ----------------------------------------------------------------------------------
- // Handle multitasking events. These are the suspend and resume events.
- // We said in our SIZE resource that we were MultiFinder aware, therefore we have
- // to activate and deactivate windows ourselves under suspend or resume events.
-
- void DoMulti(register EventRecord *evt)
- {
- // the OS data is in the high byte
- switch ((evt->message >> 24) & 0x00FF)
- { case mouseMovedMessage:
- DoIdle(); // mouse moved is also an idle event
- break;
- case suspendResumeMessage:
-
- gInBack = (evt->message & resumeFlag) == 0;
- if (FrontWindow()) {
- if (gInBack)
- DeactivateWindow((WindowPtr )FrontWindow());
- else
- ActivateWindow((WindowPtr)FrontWindow());
- DrawClippedGrowIcon(FrontWindow());
- }
- break;
- } // switch
-
- } // DoMulti
-
- // ----------------------------------------------------------------------------------
- // DoClick
- // ----------------------------------------------------------------------------------
- // Handle a mouse down event.
-
- void DoClick(register EventRecord *evt)
- {
- WindowPtr theWindow;
-
- switch(FindWindow(evt->where, &theWindow))
- {
- case inDesk: break;
- case inMenuBar: DoMenu(MenuSelect(evt->where));
- break;
- case inSysWindow: SystemClick(evt,theWindow);
- break;
- case inContent: DoClickInContent(evt,theWindow);
- break;
- case inDrag: DoDragWindow(evt,theWindow);
- break;
- case inGrow: DoGrowWindow(evt,theWindow);
- break;
- case inGoAway: DoCloseWindow(evt,theWindow);
- break;
- case inZoomIn: DoZoom(evt,theWindow,inZoomIn);
- break;
- case inZoomOut: DoZoom(evt,theWindow,inZoomOut);
- break;
- default: break;
-
- } // switch
- } // DoClick
-
-
- // ----------------------------------------------------------------------------------
- // DoEvent
- // ----------------------------------------------------------------------------------
- // Handle an event.
-
- void DoEvent(void)
- {
- EventRecord theEvent;
- Boolean eventOccured;
-
- while (!gQuit)
- {
- // Support old macs here
- if(gHaveWNE)
- eventOccured = WaitNextEvent(everyEvent,&theEvent,10,nil);
- else {
- SystemTask();
- eventOccured = GetNextEvent(everyEvent, &theEvent);
- } // if/else
-
- // if we have a text window call the idle procedure for it
- if (gDocWindow)
- { TEIdle( GetTE(gDocWindow));
- } // if
-
- // handle the events we worry about
-
- if(eventOccured) {
- switch(theEvent.what) {
- case nullEvent: break;
- case mouseDown: DoClick(&theEvent); break;
- case mouseUp: break;
- case keyDown: DoKey(&theEvent); break;
- case keyUp: break;
- case autoKey: DoKey(&theEvent); break;
- case updateEvt: DoUpdate(&theEvent); break;
- case diskEvt: break;
- case activateEvt: DoActivate(&theEvent); break;
- case networkEvt: break;
- case driverEvt: break;
- case app1Evt: break;
- case app2Evt: break;
- case app3Evt: break;
- case osEvt: DoMulti(&theEvent); break;
- case kHighLevelEvent: DoHighLevel(&theEvent); break;
- default: break;
- } // switch
-
- } else
- {
- if (gDocWindow)
- {
- WindowCursor(gDocWindow);
- } // if
- } // if/else
- } // while
- } // DoEvent
-
-
-